<?php
// 显示所有错误
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// 加载数据库配置
require_once __DIR__ . '/../config/database.php';

echo "<h2>数据库配置信息:</h2>";
echo "主机: " . DB_HOST . "<br>";
echo "用户名: " . DB_USERNAME . "<br>";
echo "数据库: " . DB_NAME . "<br>";
echo "表前缀: " . DB_PREFIX . "<br>";

try {
    // 创建PDO连接
    $dsn = "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET;
    $pdo = new PDO($dsn, DB_USERNAME, DB_PASSWORD);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "<h2>数据库连接成功!</h2>";
    
    // 检查用户表是否存在
    $stmt = $pdo->query("SHOW TABLES LIKE '".DB_PREFIX."users'");
    $tableExists = $stmt->rowCount() > 0;
    
    if ($tableExists) {
        echo "<h3>用户表存在</h3>";
        
        // 检查管理员用户
        $stmt = $pdo->prepare("SELECT * FROM ".DB_PREFIX."users WHERE role = 'admin'");
        $stmt->execute();
        $adminUsers = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        if (count($adminUsers) > 0) {
            echo "<h3>找到 " . count($adminUsers) . " 个管理员用户:</h3>";
            echo "<table border='1'>";
            echo "<tr><th>ID</th><th>用户名</th><th>邮箱</th><th>角色</th><th>密码哈希</th></tr>";
            
            foreach ($adminUsers as $user) {
                echo "<tr>";
                echo "<td>" . $user['id'] . "</td>";
                echo "<td>" . $user['username'] . "</td>";
                echo "<td>" . $user['email'] . "</td>";
                echo "<td>" . $user['role'] . "</td>";
                echo "<td>" . $user['password'] . "</td>";
                echo "</tr>";
            }
            
            echo "</table>";
            
            // 测试密码验证
            $testPassword = 'admin123';
            foreach ($adminUsers as $user) {
                echo "<h3>测试密码验证:</h3>";
                echo "用户名: " . $user['username'] . "<br>";
                echo "测试密码: " . $testPassword . "<br>";
                $passwordVerified = password_verify($testPassword, $user['password']);
                echo "密码验证结果: " . ($passwordVerified ? "通过" : "失败") . "<br>";
            }
        } else {
            echo "<h3>没有找到管理员用户!</h3>";
        }
    } else {
        echo "<h3>用户表不存在!</h3>";
    }
    
    // 测试创建一个新的管理员用户
    echo "<h2>创建新的管理员用户:</h2>";
    
    // 先删除已有的test_admin用户
    $stmt = $pdo->prepare("DELETE FROM ".DB_PREFIX."users WHERE username = ?");
    $stmt->execute(['test_admin']);
    
    // 创建新用户
    $username = 'test_admin';
    $password = 'admin123';
    $password_hash = password_hash($password, PASSWORD_DEFAULT);
    
    echo "用户名: " . $username . "<br>";
    echo "密码: " . $password . "<br>";
    echo "密码哈希: " . $password_hash . "<br>";
    
    // 插入用户
    $stmt = $pdo->prepare("INSERT INTO ".DB_PREFIX."users (username, password, email, role, status, created_at, updated_at) 
                          VALUES (?, ?, ?, ?, ?, NOW(), NOW())");
    $result = $stmt->execute([$username, $password_hash, 'test_admin@example.com', 'admin', 'active']);
    
    echo "创建结果: " . ($result ? "成功" : "失败") . "<br>";
    echo "新用户ID: " . $pdo->lastInsertId() . "<br>";
    
} catch (PDOException $e) {
    echo "<h2>数据库错误:</h2>";
    echo $e->getMessage();
}
?> 